package com.jkhan.demo.ioflow;

import java.io.*;

/**
 * @ClassName InputStreamReaderTest
 * @Description InputStreamReaderTest
 * @Author hjk
 * @Date 2022/2/21 21:07
 **/
public class InputStreamReaderTest {
    public static void main(String[] args) {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream("hello.txt");
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "gbk");
            fileOutputStream = new FileOutputStream("hello_gbk.txt");
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "utf-8");
            char[] chars = new char[20];
            int len;
            while ((len = inputStreamReader.read(chars)) != -1) {
                String str = new String(chars, 0, len);
                System.out.print(str);
                for (int i = 0; i < len; i++) {
                    outputStreamWriter.write(chars[i]);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
